﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
$(document).ready(function() {
    SetupSingleBills();
    SetupBanks();
    SetupdivLines();
    SetupReceiveBill();
});

function SetupSingleBills() {
    $(".singleBill")
        .draggable({ revert: 'invalid', helper: 'clone' })
        .each(function(index) {
            $(this).attr("id", index);
        })
        .mouseover(function() {
            $(this).addClass("selected");
        })
        .mouseout(function() {
            $(this).removeClass("selected");
        });
}

function SetupdivLines() {
    $(".lineActionMenu").hide();
    $(".divLine")
        .draggable({ revert: 'invalid', helper: 'clone' })
        .each(function(index) {
            $(this).attr("id", index);
        })
        .mouseover(function() {
            $(this).addClass("selected");
            $(this).children(".lineActionMenu").show();
        })
        .mouseout(function() {
            $(this).removeClass("selected");
            $(this).children(".lineActionMenu").hide();
        });
}

function SetupBanks() {
    $(".cashFlowPanel").droppable({
        over: function(event, ui) {
            $(this).addClass("selected");
        },
        out: function(event, ui) {
            $(this).removeClass("selected");
        },
        drop: function(event, ui) {

            PostbackNewBankAssociation(
            $(ui.draggable).children("[type=hidden]").val(),
            $(this).children("[type=hidden]").val());
        }
    });
}

function SetupReceiveBill() {
    $(".receiveBill").droppable({
        over: function(event, ui) {
            $(this).addClass("selected");
        },
        out: function(event, ui) {
            $(this).removeClass("selected");
        },
        drop: function(event, ui) {
            PostbackNewBillAssociation(
                $(ui.draggable).children("[type=hidden]").val(),
                $(this).children("[type=hidden]").val());
        }
    });
}

function PostbackNewBillAssociation(billToBePaid, billToPayWith) {
    ShowBlock();
    $("#billToBePaid").val(billToBePaid);
    $("#billToPayWith").val(billToPayWith);
    $('#billForm').submit();
}

function PostbackNewBankAssociation(billId, bankId) {
    ShowBlock();
    $("#billId").val(billId);
    $("#bankId").val(bankId);
    $('#bankForm').submit();
}

function AdjustDynamicallyAssociation() {
    $(this).removeClass("selected");

    newBill =
                $("<div class='divLine'><div class='date'>" + $(ui.draggable).children(".date").html()
                    + "</div><div class='description'>" + $(ui.draggable).children(".description").html()
                    + "</div><div class='value'>" + $(ui.draggable).children(".value").html()
                    + "</div></div>")
                    .draggable({ revert: 'invalid', helper: 'clone' })
                    .mouseover(function() {
                        $(this).addClass("selected");
                    })
                    .mouseout(function() {
                        $(this).removeClass("selected");
                    });

    newBillDate = Date.parse($(ui.draggable).children(".date").html());
    attached = false;

    $(this).children(".divLine").each(function() {
        lineDate = $(this).children(".date").html();

        if (lineDate != "Data" && !attached && newBillDate < Date.parse(lineDate)) {
            attached = true;
            $(this).before(newBill);
        }
    })

    if (!attached) {
        $(this).append(newBill);
    }

    $(ui.draggable).addClass('toBeKilled');
}